package com.xxwy.ddu.common.autowired.base;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xxwy.ddu.common.util.RUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.sql.Wrapper;
import java.util.List;

/**
 * @author xxwy
 * on 2018/9/3 0003
 */
@RestController
public abstract class XxwyBaseController<E, ID extends Serializable> {

    /**
     * 获取service
     *
     * @return
     */
    public abstract XxwyBaseService<E, ID> getService();

    /**
     * 通过id获取
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/base/{id}")
    public ResponseEntity<ModelMap> get(@PathVariable ID id) {
        E entity = getService().getByid(id);
        return RUtil.success(entity);
    }

    /**
     * 获取全部数据
     * @return
     */
    @GetMapping(value = "/baseAll")
    public ResponseEntity getAll() {
        List<E> list = getService().getAll();
        return RUtil.success(list);
    }

    /**
     * 分页获取
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/basePage/{limit}/{pageNum}")
    public ResponseEntity getByPage(@PathVariable Integer pageNum, @PathVariable Integer limit) {
        Page<E> data = getService().getPage(new Page(pageNum,limit));
        return RUtil.success(data);
    }

    /**
     * 保存数据
     * @param entity
     * @return
     */
    @PostMapping(value = "/base")
    public ResponseEntity save(@RequestBody E entity) {
        getService().save(entity);
        return RUtil.success();
    }

    /**
     * 更新数据
     * @param entity
     * @return
     */
    @PutMapping(value = "/base")
    public ResponseEntity update(@RequestBody E entity) {
        getService().updateById(entity);
        return RUtil.success();
    }



    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/base/{id}")
    public ResponseEntity delAllByIds(@PathVariable ID id) {
        getService().delete(id);
        return RUtil.success();
    }



}